<?php

/**
* Profile
* @package Congregation
* @Copyright (C) 12/04/2006 heriniaina.eugene
* @ All rights reserved
* @ Released copyrighted
* @version 0.1
**/



defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

require_once( $mosConfig_absolute_path."/administrator/components/com_congregation/congregation.class.php");
require_once( $mosConfig_absolute_path."/components/com_congregation/congregation.html.php");


$task	= trim( mosGetParam( $_REQUEST, 'task'	,	""		) );
$Itemid	= trim( mosGetParam( $_REQUEST, 'Itemid'	,	""		) );
$option	= trim( mosGetParam( $_REQUEST, 'option',	""		) );
$rub	= trim( mosGetParam( $_REQUEST, 'rub'	,	""		) );
$lang	= trim( mosGetParam( $_REQUEST, 'lang'	,	"en"	) );
$username		= trim( mosGetParam( $_REQUEST, 'username'	,	""		) );

$p_id	= trim( mosGetParam( $_REQUEST, 'p_id'	,	""		) );
$c_id	= trim( mosGetParam( $_REQUEST, 'c_id'	,	""		) );
$id		= trim( mosGetParam( $_REQUEST, 'c_id'	,	0		) );

// Parameters

/*
$menu = new mosMenu( $database );
$menu->load( $Itemid );
$params = new mosParameters( $menu->params );
$params->def( 'header', $menu->name );
*/

if (file_exists( $mosConfig_absolute_path."/components/com_congregation/language/lang.".$lang.".php" )){
        include($mosConfig_absolute_path."/components/com_congregation/language/lang.".$lang.".php");
}else{
        include($mosConfig_absolute_path."/components/com_congregation/language/lang.en.php");
}



// Editor usertype check
$access = new stdClass();

$access->canEdit = $acl->acl_check( 'action', 'edit', 'users', $my->usertype, 'content', 'all' );
$access->canEditOwn = $acl->acl_check( 'action', 'edit', 'users', $my->usertype, 'content', 'own' );


switch($rub) {
	case 'status':
		include($mosConfig_absolute_path."/components/com_congregation/includes/status.php");
		break;
	case 'necro':
		include($mosConfig_absolute_path."/components/com_congregation/includes/necro.php");
		break;
	case 'casa':
		switch($task) {
			case 'delete':
				casa_delete( $option, $c_id );
				break;
			case 'orderup':
				casa_order( $c_id, $p_id, -1, $option );
				break;
			case 'orderdown':
				casa_order( $c_id, $p_id, 1, $option );
				break;
			case 'new';
			case 'edit':
				casa_edit( $option, $c_id, $p_id );
				break;
			case 'save':
				casa_save( $option, $c_id );
				break;
			case 'list':
			default:
			case 'view':
				casa_view( $option, $c_id );
				break;
		}
		break;
	default:
	case "province":
		switch($task) {
			case 'delete':
				province_delete(  $option, $p_id );
			case 'orderup':
				province_order( $p_id, -1, $option );
				break;
			case 'orderdown':
				province_order( $p_id, 1, $option );
				break;
			case "search":
				break;
			case "new":
			case "edit":
				province_edit( $option, $p_id );
				break;
			case "save":
				province_save( $option, $p_id );
				break;
			case "list":
			default:
				if($p_id) {
					province_view( $option, $p_id );
				} else {
					province_list( $option );
				}
				break;
		}
		break;
	case "profile":
		profile_edit( $option, $username );
		break;
	case "member":
		switch ($task) {
			case 'delete':
				profile_delete( $option, $username );
				break;
			case "search":
				profile_search_form( $option );
				break;
			case "save":

				profile_save( $option );
				break;
			case 'profile':
			case "edit":
				profile_edit( $option, $username );
				break;
			default:
				if($username) {
					profile_show( $option, $username );
				} else {
					profile_list( $option );
				}
				break;
		}

		break;
}




/**
* Moves the order of a record
* @param integer The increment to reorder by
*/
function casa_order( $uid, $p_id, $inc, $option ) {
	global $database, $rub, $Itemid;

	$row = new congregationCasa( $database );
	$row->load( $uid );

	$row->move( $inc, "p_id = $p_id" );

	mosRedirect( sefRelToAbs("index.php?option=$option&rub=province&task=view&p_id=$p_id&Itemid=$Itemid") );
}

function casa_delete(  $option, $c_id ) {
	global $database, $access;
	if(!$access->canEdit) {
		mosNotAuth();
		return;
	}
	$row = new congregationCasa( $database );
	$row->load( $c_id );

	if(!$row->c_id) {
		echo "<script> alert('". _Casa_Not_Found ."'); window.history.go(-1); </script>n";
		exit();
	}
	$query = "SELECT count(*) FROM #__congregation_profile WHERE c_id='$row->c_id'";
	$database->setQuery( $query );

	$members = $database->loadResult();

	if($members > 0 ) {
		echo "<script> alert('". _Casa_Has_Members ."'); window.history.go(-1); </script>n";
		exit();
	}
	$query = "DELETE FROM #__congregation_casa"
	. "\n WHERE c_id = '$c_id' "
	;
	$database->setQuery( $query );
	if (!$database->query()) {
		echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
	}
	mosRedirect( sefRelToAbs("index.php?option=$option&rub=province&p_id=$row->p_id&Itemid=$Itemid"));


}

function casa_view( $option, $c_id ) {
	global $database, $my;
	if(!$my->username) {
		mosNotAuth();
		return;
	}

	$row = new congregationCasa( $database );
	$row->load( $c_id );

	if(!$row->c_id) {
		echo "<script> alert('". _Casa_Not_Found ."'); window.history.go(-1); </script>n";
		exit();
	}

	$query = "SELECT p_name FROM #__congregation_province WHERE p_id='$row->p_id'";
	$database->setQuery( $query );

	$row->c_pname = $database->loadResult();

	$query = "SELECT u.username as lusername, u.email as lemail, p.* FROM #__users AS u LEFT JOIN #__congregation_profile AS p "
	. "\nON u.username=p.username WHERE p.c_id='$c_id' AND p.status > 0 AND p.ddate = '0000-00-00' ";

	$database->setQuery( $query );
	$row->c_members = $database->loadObjectList();

  HTML_congregation::casa_view( $row, $option );


}



function casa_save( $option, $c_id ) {
	global $Itemid, $rub, $database;
	global $database, $my, $mosConfig_absolute_path, $mosConfig_live_site;

	$row = new congregationCasa( $database );
	if (!$row->bind( $_POST )) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	if (!$row->check()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}


///// sary insertion
$imagefile_name = $_FILES['imagefile']['tmp_name'];
$imagefile = $_FILES['imagefile']['name'];

	if (@is_uploaded_file($imagefile_name)) {

		if (!eregi("(\.jpg|\.jpeg|\.png|\.gif)$", $imagefile)){
			$errmsg .= "<li> ". _Congregation_Image_Error . " " . $imagefile ."</li>";
		} else {
			// photodir
			if(is_dir($mosConfig_absolute_path . "/media/casa")) {
				$photodir = $mosConfig_absolute_path . "/media/casa/";
				$photohtml = $mosConfig_live_site . "/media/casa/";
			} elseif(mkdir($mosConfig_absolute_path . "/media/casa")) {
				$photodir = $mosConfig_absolute_path . "/media/casa/";
				$photohtml = $mosConfig_live_site . "/media/casa/";
			} else {
				$photodir = $mosConfig_absolute_path . "/media/";
				$photohtml = $mosConfig_live_site . "/media/";
			}



			if(@move_uploaded_file($imagefile_name, $photodir . $imagefile)) {
				$size = GetImageSize($photodir . $imagefile);
				$imagewidth = $size[0];
				$imageheight = $size[1];
				$imagetype = $size[2];

				/*
				if($imagewidth > 300) {
					$new_w = 300;
					$ratio = ($imagewidth / $new_w);

					$new_h = round($imageheight / $ratio);
					$pic_thumb = 1;

					if ($imagetype=="2") {
					$src_img = imagecreatefromjpeg($photodir . $imagefile);
					$dst_img = ImageCreateTrueColor($new_w,$new_h);
					imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img), imagesy($src_img));
					imagejpeg($dst_img, $photodir . "s".$imagefile);
					} elseif  ($imagetype=="3") {
					$src_img=ImageCreateFrompng($photodir . $imagefile);
					$dst_img=ImageCreateTrueColor($new_w,$new_h);
					ImageCopyResized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img), ImageSY($src_img));
					Imagepng($dst_img, $photodir . "s".$imagefile);
					}

				}
				$new_w = $imagewidth;
				$new_h = $imageheight;
				$pic_thumb = 1;

				//unlink($photodir . $saryfile);
				*/
				$row->c_picfile = $imagefile;
			} else {
				$errmsg .= "<li> ". _Congregation_Image_Error ."</li>";
			}
		}
	}
///// sary insertion end



	if (!$row->store()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}


	mosRedirect( sefRelToAbs("index.php?option=$option&rub=province&task=view&Itemid=$Itemid&p_id=$row->p_id"), "Saved" );


}

function casa_edit( $option, $c_id, $p_id = "" ) {
	global $my, $access, $database;

	if(!$access->canEdit || !$my->username) {
		mosNotAuth();
		return;
	}

	// load provinces

	$query  = "SELECT * FROM #__congregation_province ORDER BY ordering";
	$database->setQuery( $query );
	$provinces = $database->loadObjectList();

	$row = new congregationCasa( $database );
	$row->load( $c_id );

	$lists = array();

	// build the html select list for ordering
	$query = "SELECT ordering AS value, c_name AS text"
	. "\n FROM #__congregation_casa"
	. "\n ORDER BY ordering "
	;
	$lists['ordering'] 		= mosAdminMenus::SpecificOrdering( $row, $c_id, $query, 1 );

	if($p_id != "") {
		$row->p_id = $p_id;
	}

  HTML_congregation::casa_edit( $row, $provinces, $lists, $option );
}


//manage provinces

function province_delete(  $option, $p_id ) {
	global $database, $access;
	if(!$access->canEdit) {
		mosNotAuth();
		return;
	}
	$row = new congregationProvince( $database );
	$row->load( $p_id );

	if(!$row->p_id) {
		echo "<script> alert('". _Province_Not_Found ."'); window.history.go(-1); </script>n";
		exit();
	}
	$query = "SELECT count(*) FROM #__congregation_casa WHERE p_id='$row->p_id'";
	$database->setQuery( $query );

	$case = $database->loadResult();

	if($case > 0 ) {
		echo "<script> alert('". _Province_Has_Houses ."'); window.history.go(-1); </script>n";
		exit();
	}
	$query = "DELETE FROM #__congregation_province "
	. "\n WHERE p_id = '$p_id' "
	;
	$database->setQuery( $query );
	if (!$database->query()) {
		echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
	}
	mosRedirect( sefRelToAbs("index.php?option=$option&rub=province&Itemid=$Itemid"));

}


function province_view( $option, $p_id ) {
	global $my, $access, $database;

	$row = new congregationProvince( $database );
	$row->load( $p_id );

	$query = "SELECT c.*, count(p.username) AS c_members FROM #__congregation_casa AS c LEFT JOIN #__congregation_profile AS p ON c.c_id=p.c_id WHERE c.p_id='$row->p_id' AND p.status > 0 AND p.ddate = '0000-00-00' GROUP BY c.c_id ORDER BY c.ordering";
	//$query = "SELECT c.*, count(p.username) AS c_members FROM #__congregation_casa AS c LEFT JOIN #__congregation_profile AS p ON c.c_id=p.c_id WHERE c.p_id='$row->p_id' GROUP BY c.c_id ORDER BY c.ordering";
	$database->setQuery( $query );

	$row->case = $database->loadObjectList();

	HTML_congregation::province_view( $row, $option );
}

/**
* Moves the order of a record
* @param integer The increment to reorder by
*/
function province_order( $uid, $inc, $option ) {
	global $database, $option, $rub, $Itemid;

	$row = new congregationProvince( $database );
	$row->load( $uid );
	$row->move( $inc );

	mosRedirect( sefRelToAbs("index.php?option=$option&rub=$rub&Itemid=$Itemid") );
}

function province_list( $option ) {
	global $database;

//for listing
	$limit 		= intval( mosGetParam( $_REQUEST, 'limit', 20 ) );
	$limitstart = intval( mosGetParam( $_REQUEST, 'limitstart', 0 ) );

	$query = "SELECT count(*) FROM #__congregation_province ";

	$database->setQuery( $query );
	$total = $database->loadResult();

	if ( $total <= $limit ) $limitstart = 0;

	require_once( $GLOBALS['mosConfig_absolute_path'] . '/includes/pageNavigation.php' );

	$pageNav = new mosPageNav( $total, $limitstart, $limit  );

/*
	$query = "SELECT #__congregation_province.*, count(#__congregation_casa.c_id) AS p_case FROM #__congregation_province LEFT JOIN #__congregation_casa on #__congregation_province.p_id=#__congregation_casa.p_id GROUP BY #__congregation_casa.c_id "
	. "\nORDER BY ordering "
	. "\nLIMIT ".$pageNav->limitstart.",".$pageNav->limit
	;
*/
	$query = "SELECT #__congregation_province.*, count(#__congregation_casa.c_id) AS p_case FROM #__congregation_province LEFT JOIN #__congregation_casa on #__congregation_province.p_id=#__congregation_casa.p_id GROUP BY #__congregation_province.p_id "
	. "\nORDER BY ordering "
	. "\nLIMIT ".$pageNav->limitstart.",".$pageNav->limit
	;



	/*

	$query = "SELECT * FROM #__congregation_province "
	. "\nORDER BY ordering "
	. "\nLIMIT ".$pageNav->limitstart.",".$pageNav->limit
	;
	*/


	$database->setQuery( $query );
	$rows =  $database->loadObjectList();

	HTML_congregation::province_list( $rows, $option, $pageNav );

}
function province_edit( $option, $p_id ) {
	global $my, $access, $database;

	if(!$access->canEdit || !$my->username) {
		mosNotAuth();
		return;
	}

	$row = new congregationProvince( $database );
	$row->load( $p_id );

	$lists = array();

	// build the html select list for ordering
	$query = "SELECT ordering AS value, p_name AS text"
	. "\n FROM #__congregation_province"
	. "\n ORDER BY ordering "
	;
	$lists['ordering'] 		= mosAdminMenus::SpecificOrdering( $row, $p_id, $query, 1 );

  HTML_congregation::province_edit( $row, $option, $lists );
}

function province_save( $option, $p_id ) {
	global $Itemid, $rub, $database;

	$row = new congregationProvince( $database );
	if (!$row->bind( $_POST )) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	if (!$row->check()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}

	if (!$row->store()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}


	mosRedirect( sefRelToAbs("index.php?option=$option&rub=$rub&Itemid=$Itemid"), "Saved" );


}


function profile_delete( $option, $username ) {
	global $database, $access;

	if(!$access->canEdit) {
		mosNotAuth();
		return;
	}
	if ( $username ) {
		$row = new congregationMember( $database );
		$row->load( $username );

		$query = "DELETE FROM #__congregation_profile"
		. "\n WHERE username = '$username' "
		;
		$database->setQuery( $query );
		if (!$database->query()) {
			echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
		}
		mosRedirect( sefRelToAbs("index.php?option=$option&rub=casa&c_id=$row->c_id&Itemid=$Itemid"));
	} else {
		echo "<script> alert('". _Profile_Not_Found ."'); window.history.go(-1); </script>\n";
	}
}


//manage members
function profile_search_form( $option ) {
	global $database, $my;
	if(!$my->username) {
		mosNotAuth();
		return;
	}

	//load used countries
	$query  = "SELECT nationality FROM #__congregation_profile GROUP BY nationality ORDER BY nationality";
	$database->setQuery( $query );
	$nations = $database->loadObjectList();

	//load languages
	$languages=null;
	$database->setQuery( "SELECT l.*"
		. "\nFROM #__languages AS l"
		. "\nORDER BY l.active desc, l.ordering, l.name"
	);
	$languages = $database->loadObjectList();

	HTML_congregation::profile_search_form( $nations, $languages, $option );
}

function profile_show( $option, $username ) {
	global $database, $my;
	if(!$my->username) {
		mosNotAuth();
		return;
	}

	/*
	$row = new congregationMember( $database );
	$row->load( $username );

	if(!$row->username) {
		echo "<script> alert('". _Profile_Not_Found ."'); window.history.go(-1); </script>n";
		exit();
	}
	*/

	$query = "SELECT p.*, u.email, l.name as language, c.* , s.abbr as status_abbr, s.text as status_text "
	. "\nFROM #__congregation_profile AS p "
	. "\nLEFT JOIN #__users AS u ON p.username=u.username "
	. "\nLEFT JOIN #__languages AS l ON p.language=l.iso "
	. "\nLEFT JOIN #__congregation_status AS s ON p.status=s.id "
	. "\nLEFT JOIN #__congregation_casa AS c ON p.c_id=c.c_id "
	. "\nWHERE p.username='$username'"
	;

	$database->setQuery( $query );
	$row =  $database->loadObjectList();

	HTML_congregation::profile_show( $row[0], $option );
}
function profile_list( $option ) {
	global $database;

	$nationality	= trim( mosGetParam( $_REQUEST, 'nationality'	,	""		) );
	$language		= trim( mosGetParam( $_REQUEST, 'language'		,	""		) );
	$picfile		= trim( mosGetParam( $_REQUEST, 'picfile'		,	""		) );
	$txtsearch		= trim( mosGetParam( $_REQUEST, 'txtsearch'		,	""		) );


//for listing
	$limit 		= intval( mosGetParam( $_REQUEST, 'limit', 20 ) );
	$limitstart = intval( mosGetParam( $_REQUEST, 'limitstart', 0 ) );


	$where = array();


	$where[] = " p.status > 0 ";


    if($nationality) {
    	$where[] = " p.nationality='$nationality' ";
    }
    if($language) {
    	$where[] = " p.language='$language' ";
    }
    if($picfile) {
    	$where[] = " p.picfile<>'' ";
   }

    if($txtsearch) {
		$tmp = " (p.lname LIKE '%". ereg_replace(" ", "%' OR p.lname LIKE '%", $txtsearch). "%' ";
		$tmp .= " OR p.fname LIKE '%". ereg_replace(" ", "%' OR p.fname LIKE '%", $txtsearch). "%' ";
		$tmp .= " OR u.username LIKE '%". ereg_replace(" ", "%' OR u.username LIKE '%", $txtsearch). "%') ";
    	$where[] = $tmp;
   }

	$query = "SELECT count(*) FROM #__users AS u LEFT JOIN #__congregation_profile AS p "
	. "\nON u.username=p.username  "
	. (count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "")
	;


	$database->setQuery( $query );
	$total = $database->loadResult();

	if ( $total <= $limit ) $limitstart = 0;

	require_once( $GLOBALS['mosConfig_absolute_path'] . '/includes/pageNavigation.php' );

	$pageNav = new mosPageNav( $total, $limitstart, $limit  );

	$query = "SELECT u.username as lusername, u.email as lemail, p.* FROM #__users AS u LEFT JOIN #__congregation_profile AS p "
	. "\nON u.username=p.username "
	. (count( $where ) ? "\n WHERE " . implode( ' AND ', $where ) : "")
	. "\nORDER BY lname "
	. "\nLIMIT ".$pageNav->limitstart.",".$pageNav->limit
	;

	$database->setQuery( $query );
	$rows = $database->loadObjectList();


	HTML_congregation::profile_list( $rows, $option , $pageNav);

}

function profile_save( $option ) {
  global $database, $my, $mosConfig_absolute_path, $mosConfig_live_site, $Itemid;

	$newpass = mosGetParam( $_REQUEST, 'newpass') ;
	$verifypass = mosGetParam( $_REQUEST, 'verifypass') ;


	if(trim($newpass) != '' && $newpass != $verifypass) {
		echo "<script> alert('". _Profile_Pass_Not_Verified ."'); window.history.go(-1); </script>n";
		exit();
	}

	if(trim($newpass) != '') {
		//change password
		$query = "UPDATE #__users SET password='" . md5($newpass) . "' WHERE username='" . $my->username . "' LIMIT 1 ";
		echo($query);
		$database->setQuery($query);
		$database->query();
	}

	$b_year = mosGetParam( $_REQUEST, 'b_year', array() ) ;
	$b_month = mosGetParam( $_REQUEST, 'b_month', array());
	$b_day = mosGetParam( $_REQUEST, 'b_day', array() ) ;
	$b_place = mosGetParam( $_REQUEST, 'b_place', array() );
	$removepic = mosGetParam( $_REQUEST, 'removepic', '0');

	$row = new congregationMember( $database );

	if (!$row->bind( $_POST )) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	if($removepic == '1') {
		$row->picfile = "";
	}


	$row->bdate = $b_year[0]."-".$b_month[0]."-".$b_day[0];
	$row->bplace = $b_place[0];
	$row->edate = $b_year[1]."-".$b_month[1]."-".$b_day[1];
	$row->eplace = $b_place[1];
	$row->ndate = $b_year[2]."-".$b_month[2]."-".$b_day[2];
	$row->nplace = $b_place[2];
	$row->vdate = $b_year[3]."-".$b_month[3]."-".$b_day[3];
	$row->vplace = $b_place[3];
	$row->fdate = $b_year[4]."-".$b_month[4]."-".$b_day[4];
	$row->fplace = $b_place[4];
	$row->odate = $b_year[5]."-".$b_month[5]."-".$b_day[5];
	$row->oplace = $b_place[5];
	if($b_year[6])
	{
	$row->ddate = $b_year[6]."-".$b_month[6]."-".$b_day[6];
	$row->dplace = $b_place[6];
	}

	if (!$row->check()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}


///// sary insertion
$imagefile_name = $_FILES['imagefile']['tmp_name'];
$imagefile = $_FILES['imagefile']['name'];

if (@is_uploaded_file($imagefile_name)) {

	if (!eregi("(\.jpg|\.jpeg|\.png|\.gif)$", $imagefile)){
		$errmsg .= "<li> ". _Congregation_Image_Error . " " . $imagefile ."</li>";
	} else {
		// photodir
			// photodir
			if(is_dir($mosConfig_absolute_path . "/media/profiles")) {
				$photodir = $mosConfig_absolute_path . "/media/profiles/";
				$photohtml = $mosConfig_live_site . "/media/profiles/";
			} elseif(mkdir($mosConfig_absolute_path . "/media/profiles")) {
				$photodir = $mosConfig_absolute_path . "/media/profiles/";
				$photohtml = $mosConfig_live_site . "/media/profiles/";
			}


				/*
				verifier-na raha azo reduiser-na
				sinon apetraka fotsiny
				*/

					if(@move_uploaded_file($imagefile_name, $photodir . "b" . $imagefile)) {
						$size = GetImageSize($photodir . "b" . $imagefile);
						$imagewidth = $size[0];
						$imageheight = $size[1];
						$imagetype = $size[2];

						if($imagewidth > 100) {
							$new_w = 100;
							$ratio = ($imagewidth / $new_w);

							$new_h = round($imageheight / $ratio);
							$pic_thumb = 1;

							if ($imagetype=="2") {
							$src_img = imagecreatefromjpeg($photodir . "b" . $imagefile);
							$dst_img = ImageCreateTrueColor($new_w,$new_h);
							imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img), imagesy($src_img));
							imagejpeg($dst_img, $photodir . "s".$imagefile);
							} elseif  ($imagetype=="3") {
							$src_img=ImageCreateFrompng($photodir . "b" . $imagefile);
							$dst_img=ImageCreateTrueColor($new_w,$new_h);
							ImageCopyResized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,ImageSX($src_img), ImageSY($src_img));
							Imagepng($dst_img, $photodir . "s".$imagefile);
							}

						}
						$new_w = $imagewidth;
						$new_h = $imageheight;
						$pic_thumb = 1;

						//unlink($photodir . $saryfile);

						$row->picfile = $imagefile;
					} else {
						$errmsg .= "<li> ".$str[pic_error]."</li>";
					}
	}
	}
///// sary insertion end



	/*
	if (!$row->check()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}
	*/
	if (!$row->store()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>n";
		exit();
	}

	mosRedirect( sefRelToAbs("index.php?option=$option&rub=member&task=profile&username={$row->username}&Itemid=$Itemid"), _Profile_Saved );

}


function profile_edit( $option, $username ) {
	global $database, $my, $mainframe, $access;

	if(!$my->username) {
		mosNotAuth();
		return;
	}
	if(!$username) {
		$username = $my->username;
	}

	if(!$access->canEdit && $username != $my->username) {
		mosNotAuth();
		return;
	}

	
	$row = new congregationMember( $database );
	$row->load( $username );

	if(!$row->username) {
		$query = "INSERT INTO #__congregation_profile (username) VALUES ('$username')";
		$database->setQuery($query);
		$database->query();
		$row->username = $username;
	}


	$lists = array();
	//load languages

	$database->setQuery( "SELECT l.*"
		. "\nFROM #__languages AS l"
		. "\nORDER BY l.active desc, l.ordering, l.name"
	);
	$lists['language'] = $database->loadObjectList();


	//load houses
	$database->setQuery( "SELECT * FROM #__congregation_casa ORDER BY p_id, c_city");

	$lists['casa'] = $database->loadObjectList();

	//load status
	$database->setQuery( "SELECT * FROM #__congregation_status ORDER BY ordering");

	$lists['status'] = $database->loadObjectList();


  HTML_congregation::profile_edit( $row, $option, $lists);
}


